//
//  SignUpInformationViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/2.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "SignUpInformationViewController.h"
#import "OrderCommitCell.h"
#import "CommitOrderViewController.h"
@interface SignUpInformationViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    NSInteger totalNumber;
}

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)UICollectionViewFlowLayout *flowlayout;
@property (strong, nonatomic) NSMutableArray<CALayer *> *redLayers;


@property (strong, nonatomic)  UIImageView *shippingCartView;
@property (strong, nonatomic)  UILabel *numberLabel;

@property (strong, nonatomic)  UIView *bottomView;
@property(nonatomic,strong)UIButton *settleBtn;
@end

@implementation SignUpInformationViewController

- (NSMutableArray<CALayer *> *)redLayers {
    if (!_redLayers) {
        _redLayers = [NSMutableArray array];
    }
    return _redLayers;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报名信息";
    [self createTopUI];
    // Do any additional setup after loading the view.
}

-(void)createTopUI{
    self.view.backgroundColor = [UIColor lightGrayColor];
    UILabel *titleLabel = [[UILabel alloc] init];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(84);
        make.left.and.right.equalTo(self.view);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UILabel *timeLabel = [[UILabel alloc] init];
    [self.view addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(5);
        make.left.and.right.equalTo(self.view);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIImageView *bgimageView = [[UIImageView alloc] init];
    [self.view addSubview:bgimageView];
    [bgimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLabel.mas_bottom).offset(scaleTopAndBottom(20));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(15));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-15));
        make.height.mas_equalTo(scaleTopAndBottom(220));
    }];
    UILabel *bgtitleLabel = [[UILabel alloc] init];
    bgtitleLabel.text = @"您的参赛主题";
    bgtitleLabel.textAlignment = NSTextAlignmentCenter;
    [bgimageView addSubview:bgtitleLabel];
    [bgtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgimageView.mas_top).offset(scaleTopAndBottom(15));
        make.left.and.right.equalTo(bgimageView);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIImageView *leftView = [[UIImageView alloc] init];
    [bgimageView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgimageView.mas_left).offset(scaleLeftAndRight(50));
        make.top.equalTo(bgtitleLabel.mas_bottom).offset(scaleTopAndBottom(15));
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    UIImageView *rightView = [[UIImageView alloc] init];
    [bgimageView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgimageView.mas_right).offset(scaleLeftAndRight(-50));
        make.top.equalTo(bgtitleLabel.mas_bottom).offset(scaleTopAndBottom(15));
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    UILabel *themeleLabel = [[UILabel alloc] init];
    themeleLabel.textAlignment = NSTextAlignmentCenter;
    [bgimageView addSubview:themeleLabel];
    [themeleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgtitleLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.left.equalTo(leftView.mas_right).offset(scaleLeftAndRight(0));
        make.right.equalTo(rightView.mas_left).offset(scaleLeftAndRight(0));
        make.height.mas_equalTo(scaleTopAndBottom(30));
    }];
/********************************横线的上半部分****************************/
    UIView *lineView = [[UIView alloc] init];
    [bgimageView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgimageView.mas_left).offset(scaleLeftAndRight(20));
        make.centerY.equalTo(bgimageView.mas_centerY);
        make.right.equalTo(bgimageView.mas_right).offset(scaleLeftAndRight(-20));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        
    }];
/********************************横线的下半部分****************************/
    UILabel *nameleLabel = [[UILabel alloc] init];
    nameleLabel.textAlignment = NSTextAlignmentCenter;
    [bgimageView addSubview:nameleLabel];
    [nameleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(scaleTopAndBottom(20));
        make.left.equalTo(bgimageView.mas_left).offset(scaleLeftAndRight(0));
        make.right.equalTo(bgimageView.mas_right).offset(scaleLeftAndRight(0));
    }];
    UILabel *productleLabel = [[UILabel alloc] init];
    productleLabel.textAlignment = NSTextAlignmentCenter;
    [bgimageView addSubview:productleLabel];
    [productleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameleLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.left.equalTo(bgimageView.mas_left).offset(scaleLeftAndRight(0));
        make.right.equalTo(bgimageView.mas_right).offset(scaleLeftAndRight(0));
    }];
    UILabel *phoneLabel = [[UILabel alloc] init];
     phoneLabel.textAlignment = NSTextAlignmentCenter;
    [bgimageView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(productleLabel.mas_bottom).offset(scaleTopAndBottom(5));
        make.left.equalTo(bgimageView.mas_left).offset(scaleLeftAndRight(0));
        make.right.equalTo(bgimageView.mas_right).offset(scaleLeftAndRight(0));
        make.bottom.equalTo(bgimageView.mas_bottom).offset(scaleTopAndBottom(-20));
        make.height.equalTo(@[productleLabel,nameleLabel]);
    }];
    titleLabel.backgroundColor = [UIColor redColor];
    timeLabel.backgroundColor = [UIColor cyanColor];
    bgtitleLabel.backgroundColor = [UIColor redColor];
    leftView.backgroundColor = [UIColor redColor];
    rightView.backgroundColor = [UIColor redColor];
    themeleLabel.backgroundColor = [UIColor greenColor];
    
    lineView.backgroundColor = [UIColor blackColor];
    bgimageView.backgroundColor = [UIColor orangeColor];
    
    nameleLabel.backgroundColor = [UIColor greenColor];
    productleLabel.backgroundColor = [UIColor redColor];
    phoneLabel.backgroundColor = [UIColor purpleColor];
    [self createCollectionView:bgimageView];
}
-(void)createCollectionView:(UIImageView *)imageview{
    self.flowlayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowlayout];
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.collectionView];
    self.flowlayout.itemSize = CGSizeMake(scaleLeftAndRight(187), scaleTopAndBottom(230));
    self.flowlayout.minimumLineSpacing = 1;
    self.flowlayout.minimumInteritemSpacing = 1;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageview.mas_bottom).offset(scaleTopAndBottom(10));
        make.left.and.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-45));
    }];
    [self.collectionView registerClass:[OrderCommitCell class] forCellWithReuseIdentifier:@"OrderCommitCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"informHeader"];
    self.bottomView = [[UIView alloc] init];
    [self.view addSubview:_bottomView];
    _bottomView.backgroundColor = [UIColor purpleColor];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom);
        make.left.and.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    self.shippingCartView = [[UIImageView alloc] init];
    self.shippingCartView.image = [UIImage imageNamed:@"购物车"];
    [_bottomView addSubview:self.shippingCartView];
    [self.shippingCartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomView.mas_left).offset(scaleLeftAndRight(10));
       make.top.equalTo(self.bottomView.mas_top).offset(scaleLeftAndRight(10));
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    self.numberLabel = [[UILabel alloc] init];
    [_bottomView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shippingCartView.mas_right).offset(scaleLeftAndRight(8));
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(144), scaleTopAndBottom(20)));
    }];
    self.settleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.settleBtn.backgroundColor = [UIColor orangeColor];
    [self.settleBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    [self.settleBtn addTarget:self action:@selector(OrderInformationpage:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:self.settleBtn];
    [self.settleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(_bottomView);
        make.right.equalTo(_bottomView.mas_right);
        make.left.equalTo(self.numberLabel.mas_right);
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    OrderCommitCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrderCommitCell" forIndexPath:indexPath];
    [cell setPlusTapHandle:^(CGPoint position) {
        totalNumber++;
        [self updateNumberLabel];
        CGPoint startPoint = [self.view convertPoint:position fromView:self.collectionView];
        CGPoint endPoint = [self.view convertPoint:self.shippingCartView.center fromView:self.bottomView];
        [self initCHLayerFromPoint:startPoint toPoint:endPoint];
        //model add to array
        
    }];
    
    [cell setMinusTapHandle:^() {
        totalNumber--;
        [self updateNumberLabel];
    }];

    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"informHeader" forIndexPath:indexPath];
    view.backgroundColor = [UIColor whiteColor];
    [self createHeaderViewAboutUI:@"赛事周边" AndAddSubviewToFatherview:view];
    return view;
}
//创建collectionview的头部视图
-(UIView *)createHeaderViewAboutUI:(NSString *)headerTitle AndAddSubviewToFatherview:(UIView *)fatherView{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor yellowColor];
    [fatherView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(fatherView.mas_centerY);
        make.left.equalTo(fatherView.mas_left);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(scaleLeftAndRight(2));
        
    }];
    UILabel *lable = [[UILabel alloc] init];
    [fatherView addSubview:lable];
    lable.text = headerTitle;
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_right).offset(scaleLeftAndRight(10));
        make.top.equalTo(fatherView.mas_top);
        make.bottom.equalTo(fatherView.mas_bottom);
        make.right.equalTo(fatherView.mas_right).offset(scaleLeftAndRight(-10));
    }];
    UIView *bottomview = [[UIView alloc] init];
    bottomview.backgroundColor = [UIColor lightGrayColor];
    [fatherView addSubview:bottomview];
    [bottomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(fatherView);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(fatherView.mas_bottom);
    }];
    return fatherView;
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, scaleTopAndBottom(50));
}
- (void)initCHLayerFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint {
    
    CALayer *chLayer = [[CALayer alloc] init];
    [self.redLayers addObject:chLayer];
    
    chLayer.frame = CGRectMake(startPoint.x, startPoint.y, 15, 15);
    chLayer.cornerRadius = CGRectGetWidth(chLayer.frame)/2.f;
    chLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:chLayer];
    
    CAKeyframeAnimation *CHAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(path, NULL, endPoint.x, startPoint.y, endPoint.x, endPoint.y);
    
    CHAnimation.path = path;
    CHAnimation.removedOnCompletion = NO;
    CHAnimation.fillMode = kCAFillModeBoth;
    CHAnimation.duration = 0.5;
    CHAnimation.delegate = self;
    
    [chLayer addAnimation:CHAnimation forKey:nil];
    
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.redLayers[0] removeFromSuperlayer];
    [self.redLayers removeObjectAtIndex:0];
    
}

- (void)updateNumberLabel{
    NSString *totalNumberStr = [NSString stringWithFormat:@"共 %zd 件商品",totalNumber];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalNumberStr];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.f] range:NSMakeRange(1, attributedStr.length - 4)];
    self.numberLabel.attributedText = attributedStr;
}
-(void)OrderInformationpage:(UIButton *)btn{
    CommitOrderViewController *commitVc = [[CommitOrderViewController alloc] init];
    [self.navigationController pushViewController:commitVc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
